package view;
//
//import javax.swing.*;
//import javax.swing.text.*;
import java.awt.Color;
import java.awt.Insets;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;




public class MyTextPane extends JTextPane{
	private static final long serialVersionUID = 1L;	
	
	private   int       jthMatch=0, nMatches;
	private   int       position=0;
	private   int[]     Position;
	
	
	
	public String splitWord= "xxx";
	public SimpleAttributeSet sas = new SimpleAttributeSet();
	
	public MyTextPane(){

	}
	public MyTextPane(StyledDocument document){

		System.out.println("Inicio do MyTextPane  ");
		this.setSize(400, 400);
		// Propriedades do textPane

		this.addCaretListener(new CurrentLineHighlighter(null));
		StyleConstants.setAlignment( sas, StyleConstants.ALIGN_JUSTIFIED);
		StyleConstants.setFontFamily(sas, "sansserif");
    	StyleConstants.setFontSize(sas, 18);
        this.setParagraphAttributes( sas, true);

		
		this.setBackground(new Color(240,248,250));      
		// Set margins inside the text pane
		Insets margin = this.setMargin(10, 20, 20, 10);
		this.setMargin(margin);
//		... Set textPane's initial text, scrolling, and border.
		this.setText("Cole, escreva ou entao importe o seu texto aqui. Paste your text here.");

		System.out.println("Fim do MyTextPane  ");
	}

	/**
	 * selectWords
	 *
	 */
	public void selectWords (){
		System.out.println("Inicio do MyTextPane -> selectWords ");
		
		String s;
		System.out.println("selectWords ok ate aqui");
		String txt= this.getText();
		if ((s=this.getSelectedText())!=null){
			this.splitWord=s;
	        position= this.getCaretPosition();//posicao do texto selecionado
		}
		else{
		}
        System.out.println("position is "+position+" splitWord is "+splitWord);
        System.out.flush();
        this.sendSelecTextToPane(txt);
        jthMatch=0;// pula de volta pro inicio        
		System.out.println("Inicio do MyTextPane -> selectWords ");
	}
	/**
	 * findWord
	 * @param splitWd
	 */
	public void findWord (String splitWd){

		System.out.println("Inicio do MyTextPane -> findWord ");
        String txt= this.getText();//recupera texto
        
        if (txt.contains(splitWd)){
        	this.splitWord=splitWd;
            position=txt.indexOf(splitWd);
            //this.setCaretPosition(position);
            this.sendSelecTextToPane(txt);
            System.out.println("findWord ok ate aqui");
            System.out.println("position is "+position+" splitWord is "+splitWd);
            jthMatch=0;// pula de volta pro in�cio
        }
        else{ // do nothing if word is not found
        	//sendTextToPane(txt);
        }  	

		System.out.println("Fim do MyTextPane -> findWord ");
	}
	//
	//*********************************************************************
	//
	public void previousWord (){
		System.out.println("Inicio do MyTextPane -> previousWord ");
		
		if (jthMatch==0) jthMatch=nMatches-1;// pula pro final
        this.setCaretPosition(Position[jthMatch--]);

		System.out.println("Fim do MyTextPane -> previousWord ");      
	}
	
	public void nextWord (){
		System.out.println("Inicio do MyTextPane -> nextWord ");
        this.setCaretPosition(Position[jthMatch++]);
        if (jthMatch==nMatches) jthMatch=0;// pula de volta pro inicio
    	System.out.println("Fim do MyTextPane -> nextWord ");
	}
	
    /**
     * reescreve o textpane com a string s
     *  @param s
     */
    public  void sendTextToPane(String s) {
    	System.out.println("Inicio do MyTextPane -> sendTextToPane ");
    	// Output to window
    	this.selectAll();
    	this.replaceSelection (""); 
    	StyledDocument document = new DefaultStyledDocument();
    	document.setParagraphAttributes(0, s.length(), sas, true);
    	this.setDocument(document);
    	int offset = 1;
    	try{
    		 document.insertString(offset, s ,sas);
    	} catch (BadLocationException ble) {
    		 ble.printStackTrace();
    	}
    	System.out.println("Fim do MyTextPane -> sendTextToPane ");
    }
    /**
     *  
     *  reescreve o textpane com a string s apresentando todas as substrings 
     *  this.splitWord destacadas 
     * @param s
     */
    public  void sendSelecTextToPane(String s) {
    	System.out.println("Inicio do MyTextPane -> sendSelecTextToPane ");
    	// Output to window
    	this.selectAll();
    	this.replaceSelection ("");
    	StyleConstants.setAlignment( sas, StyleConstants.ALIGN_JUSTIFIED);
    	//busca.textPane.setText (s);
        String[] textArray= s.split(this.splitWord);
        StyledDocument document = new DefaultStyledDocument();
    	document.setParagraphAttributes(0, s.length(), sas, true);
        this.setDocument(document);
        Position =new int[textArray.length];
        nMatches=textArray.length;
        for (int i=0; i< textArray.length; i++){
            try {
                int offset = document.getLength();
//                SimpleAttributeSet sas = new SimpleAttributeSet();
                sas.removeAttribute(StyleConstants.Background);
                document.insertString(offset, textArray[i] ,sas);
                offset = document.getLength();
                if (i<textArray.length-1){
                    sas.addAttribute(StyleConstants.Background, Color.YELLOW);
                    document.insertString(offset, this.splitWord, sas);
                    Position[i]=offset;
                }
            } catch (BadLocationException ble) {
                ble.printStackTrace();
            }
        }
             this.setCaretPosition(position);
             System.out.println("Fim do MyTextPane -> sendSelecTextToPane ");
    }
	//..........setMargins..........................................
	public Insets setMargin(int top, int left, int bottom, int right ){
		System.out.println("Inicio do MyTextPane -> setMargin ");
		
		Insets m=new Insets(top, left, bottom, right);
		System.out.println("Fim do MyTextPane -> setMargin ");
		return m;
		
	}
}